/**
 * Created by zhanglihe on 15/11/19.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'dialog',
    'model',
    'datepicker'
], function ($, common, ready, Collection, template, dialog, Model, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'site/withdraws',
                condition: {limit: 10, offset: 0},
                model: {
                    event: {
                        '.agree:click': 'agree',
                        '.reject:click': 'reject',
                        '.look_join_withdraws:click': 'look_join_withdraws'
                    },
                    agree: function (e) {
                        e.preventDefault();
                        var _this_item = this;
                        dialog({
                            content: '确定要同意<br />[' + _this_item.data.site_name + ']<br />提现<br />' + _this_item.data.amount + '?',
                            modal: true,
                            ok: function () {
                                if (!new Model({
                                        url: _global.url.api + 'site/amount',
                                        data: {'act': 'agree', 'id': _this_item.data.id}
                                    }).put()) {
                                    return false;
                                }
                                _this.collection.fetch();
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    reject: function (e) {
                        e.preventDefault();
                        var _this_item = this;
                        dialog({
                            content: '确定要拒绝<br />[' + _this_item.data.site_name + ']<br />提现<br />' + _this_item.data.amount + '?',
                            modal: true,
                            ok: function () {
                                if (!new Model({
                                        url: _global.url.api + 'site/amount',
                                        data: {'act': 'reject', 'id': _this_item.data.id}
                                    }).put()) {
                                    return false;
                                }
                                _this.collection.fetch();
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    },
                    look_join_withdraws: function (e) {
                        e.preventDefault();
                        var html = template('look_join_withdraws-tpl');
                        var dialogObj = dialog({
                            title: '提现明细(' + this.data.id + ')(提现金额:' + this.data.amount + ')',
                            content: html,
                            width: '800',
                            height: '400',
                            modal: true,
                            ok: function () {
                                _this.collection.fetch();
                            }, cancel: function () {
                            }
                        }).show();
                        dialogObj._$('content').css('overflow', 'auto');
                        show_look_join_withdraws_list(this.data);
                    }
                },
                callback: function () {
                    loaded();
                    $('.pagination').append('<li><a href="javascript:">记录总金额：￥'+_this.collection.result._amount+'</a></li>');
                }
            });
            this.collection.fetch();
        },

        event: function () {
            var _this = this;
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd'
                });
            });
            /**
             * 按状态查询
             */
            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });

            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.condition.start_time = $('input[name="begintime"]').val();
                _this.collection.condition.end_time = $('input[name="endtime"]').val();
                _this.collection.condition.type = $("#type").val();
                _this.collection.reload();
            });


        }
    };
    var show_look_join_withdraws_list = function(data){
        var collection = new Collection({
            container: 'data-look_join_withdraws',
            template: 'look_join_withdraws-data-tpl',
            url: _global.url.api + 'site/withdraws_logs',
            show_page : false,
            condition: {limit: 1000, offset: 0, site_withdraw_id: data.id},
            model: {},
            callback: function () {
                loaded();
            }
        });
        collection.fetch();
    };

    new index();

});